package com.qcby.controller;

import com.qcby.entity.UserEquip;
import com.qcby.model.AuctionHouseModel;
import com.qcby.model.Change;
import com.qcby.model.Edit;
import com.qcby.model.Page;
import com.qcby.model.ResponseModel;
import com.qcby.model.Select;
import com.qcby.service.AuctionHouseService;
import com.qcby.service.HouseService;
import com.qcby.service.SaleService;
import com.qcby.service.UserIntegralService;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Tom
 * @Date: 2021/07/20/15:43
 * @Description:
 */
@Controller
@RequestMapping("auction")
public class AuctionHouseController {
    @Autowired
  private AuctionHouseService auctionHouseService ;
    @Autowired
    private SaleService saleService;
    @Autowired
    private UserIntegralService userIntegralService;
    @Autowired
    private HttpSession session;
    @Autowired
    private HouseService houseService;
    @RequestMapping("list")
    public String show(Model model, Page page){
      Integer index= (page.getPageIndex()-1)* page.getPageSize();
      List<AuctionHouseModel> list= auctionHouseService.list(index,page.getPageSize());
      System.out.println(list);
      Integer c = auctionHouseService.selectCount();
      System.out.println(c);
      System.out.println(page.getPageSize());
      double ceil=Math.ceil(c*1.0/page.getPageSize());
      System.out.println(ceil);
      // 查询到的数据总量
      model.addAttribute("total",ceil);
      model.addAttribute("message",list);
      return "user-equip/auctionhouse";
    }

    @RequestMapping("select")
    public String select(Model model, Select select){
      System.out.println(select.getEquipName());
        AuctionHouseModel auctionHouseModel = auctionHouseService.selectByPrice(select);
        model.addAttribute("message",auctionHouseModel);
        return "user-equip/select";
    }

    @RequestMapping("toedit/{id}")
    public String toedit(Model model,@PathVariable("id") Integer id){
        AuctionHouseModel auctionHouseModel = auctionHouseService.selectById(id);
        model.addAttribute("message",auctionHouseModel);
        return "user-equip/edit";
    }
    @RequestMapping("edit")
    @ResponseBody
    public ResponseModel<UserEquip> edit (Model model, Integer id,Integer price,Integer currentPrice){
        System.out.println(id);
        System.out.println(price);
        System.out.println(currentPrice);
        saleService.updataByPriceAc(id,price,currentPrice);
        ResponseModel<UserEquip> userEquipResponseModel = new ResponseModel<>();
        userEquipResponseModel.setCode(200);
        userEquipResponseModel.setMsg("更新成功");
        return userEquipResponseModel;
    }

    @RequestMapping("buy")
    public String buy(Model model,Integer id){
        Integer i= Integer.parseInt(session.getAttribute("userId").toString());
        Integer currentMoney = userIntegralService.selectIntegralByUserId(i);
        Integer money = saleService.selectPriceById(id);
        Integer userId = saleService.selectUserIdById(id);
        Integer integral = currentMoney - money;
        Integer userMoney = userIntegralService.selectIntegralByUserId(userId);
        Integer integral1 = userMoney + money;
        userIntegralService.updataBySale(integral,i);
        userIntegralService.updataByPrice(integral1,userId);
        Integer equipId = saleService.selectEquipIdById(id);
        Change swap = new Change(userId,equipId,i);
        houseService.updataUserId(swap);
        saleService.updataByUserId(swap);
        model.addAttribute("success","购买成功");
        return "user-equip/auctionhouse";
      }

      @RequestMapping("query")
    public String query(Model model){
        Integer i= Integer.parseInt(session.getAttribute("userId").toString());
        Integer currentMoney = userIntegralService.selectIntegralByUserId(i);
        model.addAttribute("money", currentMoney);
        return "user-equip/yue";
      }
    }

